package com.lmiot.xyclick.Bean.SQL;

import android.content.Context;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.lmiot.xyclick.Bean.SQL.DaoMaster;
import com.lmiot.xyclick.Bean.SQL.FriendBeanDao;
import com.lmiot.xyclick.Util.FileUtils;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;

public class FriendBeanSqlUtil {
    private static final FriendBeanSqlUtil ourInstance = new FriendBeanSqlUtil();
    private FriendBeanDao mFriendBeanDao;

    public static FriendBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private FriendBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mFriendBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "FriendBean-db", null).getWritableDatabase()).newSession().getFriendBeanDao();
    }

    public void add(FriendBean friendBean) {
        this.mFriendBeanDao.insertOrReplace(friendBean);
        saveLocal();
    }

    public void addList(List<FriendBean> list) {
        this.mFriendBeanDao.insertOrReplaceInTx(list);
        saveLocal();
    }

    public void update(FriendBean friendBean) {
        this.mFriendBeanDao.update(friendBean);
        saveLocal();
    }

    public void delByID(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mFriendBeanDao.queryBuilder().where(FriendBeanDao.Properties.UserID.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mFriendBeanDao.delete((FriendBean) arrayList.get(0));
        }
        saveLocal();
    }

    public FriendBean searchByID(String str) {
        if (str == null) {
            return null;
        }
        ArrayList arrayList = (ArrayList) this.mFriendBeanDao.queryBuilder().where(FriendBeanDao.Properties.UserID.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            return (FriendBean) arrayList.get(0);
        }
        return null;
    }

    public List<FriendBean> searchAllUnGroup() {
        List<FriendBean> searchAll = searchAll();
        ArrayList arrayList = new ArrayList();
        if (searchAll.size() > 0) {
            for (FriendBean friendBean : searchAll) {
                if (TextUtils.isEmpty(friendBean.getGroupID())) {
                    arrayList.add(friendBean);
                } else if (friendBean.getGroupID().equals("666")) {
                    arrayList.add(friendBean);
                }
            }
        }
        return arrayList;
    }

    public List<FriendBean> searchAllByGroupID(String str) {
        List<FriendBean> list = this.mFriendBeanDao.queryBuilder().where(FriendBeanDao.Properties.GroupID.eq(str), new WhereCondition[0]).orderAsc(FriendBeanDao.Properties.Id).build().list();
        return list == null ? new ArrayList() : list;
    }

    public List<FriendBean> searchAllByGroupIDOther(String str) {
        List<FriendBean> searchAll = searchAll();
        ArrayList arrayList = new ArrayList();
        if (searchAll.size() > 0) {
            for (FriendBean friendBean : searchAll) {
                String groupID = friendBean.getGroupID();
                if (str.equals("666")) {
                    if (!TextUtils.isEmpty(groupID) && !groupID.equals(str)) {
                        arrayList.add(friendBean);
                    }
                } else if (TextUtils.isEmpty(groupID)) {
                    arrayList.add(friendBean);
                } else if (!groupID.equals(str)) {
                    arrayList.add(friendBean);
                }
            }
        }
        return arrayList;
    }

    public List<FriendBean> searchAll() {
        List<FriendBean> list = this.mFriendBeanDao.queryBuilder().orderAsc(FriendBeanDao.Properties.Id).build().list();
        return list == null ? new ArrayList() : list;
    }

    private void saveLocal() {
        try {
            FileUtils.saveToLocalData("Friend.frd", new Gson().toJson(getInstance().searchAll()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
